package sealte.wdgf.control.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sealte.wdgf.control.bean.PetBean;
import sealte.wdgf.control.servlet.system.SystemPostServlet;
import sealte.wdgf.model.pojo.Pet;

public class AddNewPetServlet extends SystemPostServlet {

	private static final long serialVersionUID = -7456733000613927633L;
	static Logger logger = LoggerFactory.getLogger(AddNewPetServlet.class);
	
	@Override
	protected void doBusinessService(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		String name = req.getParameter("name");
		String color = req.getParameter("color");
		String ageStr = req.getParameter("age");
		Pet pet = new Pet();
		pet.setName(name);
		pet.setColor(color);
		pet.setAge(Integer.valueOf(ageStr));
		ServletContext application = req.getServletContext();
		DataSource ds = (DataSource)application.getAttribute("DataSource");
		Connection conn = null;
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			logger.error("获取Connection实例时出错！原因："+e.getMessage());
		}
		PetBean pb = new PetBean(conn);
		pb.add(pet);
		try {
			if(null!=conn){
				conn.close();
			}
		} catch (SQLException e) {
			logger.error("执行Connection-close操作时出错！原因："+e.getMessage());
		}
		resp.sendRedirect("HtmlContentTransfer");
		super.doBusinessService(req, resp);
	}
	
}
